﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Mac.Foundation;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class IPadViewControl : Control
    {
        StackPanel pageControls;
        public IPadViewControl()
        {
            Items = new Collection<IPadViewItem>();

            pageControls = new StackPanel()
            {
                Orientation= Orientation.Horizontal,
                MarginBottom=10,
                ZIndex=10,
            };
        }

        private int _row = 4;
        private int _columns = 4;
        /// <summary>
        /// 矩阵的行数
        /// </summary>
        [Description("矩阵的行数"), Category("DSkin")]
        public int MatrixRows
        {
            get { return _row; }
            set
            {
                _row = value;
            }
        }

        /// <summary>
        /// 矩阵的列数
        /// </summary>
        [Description("矩阵的列数"), Category("DSkin")]
        public int MatrixColumns
        {
            get { return _columns; }
            set
            {
                _columns = value;
            }
        }

        [Description("是否可移动标签"), DefaultValue(true)]
        public bool ChangedMove
        {
            get { return GetValue<bool>(); }
            set { SetValue(value); }
        }
        public float ItemHeight
        {
            get { return GetValue<float>(); }
            set { SetValue(value); }
        }
        public float ItemWidth
        {
            get { return GetValue<float>(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 获取标签在集合中的下标，非新下标
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int GetPageIndex(IPadViewItem item)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i] == item)
                {
                    return i;
                }
            }
            return -1;
        }

        private int _pageIndex = 0;

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                float move = 0;
                if (value > _pageIndex)
                {
                    move = -(value - _pageIndex) * this.Width.Value;
                }
                else if (value < _pageIndex)
                {
                    move = (_pageIndex - value) * this.Width.Value;
                }
                _pageIndex = value;
                for (int i = 0; i < Items.Count; i++)
                {
                    Items[i].OldLocation.X = Items[i].OldLocation.X + move;
                    Items[i].Move();
                }
            }
        }
        public Collection<IPadViewItem> Items
        { 
            get { return GetValue<Collection<IPadViewItem>>(); }
            set { SetValue(value); }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            PageIndex = 0;
            Children.Add(pageControls);
        }
        public void AddTabBarItem(IPadViewItem item)
        {
            Items.Add(item);
            Children.Add(item);
            item.MarginLeft = 0;
            item.MarginTop= 0;
            item.HostTabBarControl = this;
            item.NewTabPageIndex = Items.Count - 1;

            SetPosition();
        }
        public void SetPosition()
        {
            //获取每页容纳的Item数量
            int matrixCount = MatrixRows * MatrixColumns;
            //计算总页数
            int pagecount = Items.Count / matrixCount + (Items.Count % matrixCount == 0 ? 0 : 1);
            //当前页下标
            int pageindex = 0;
            //第N行
            int rowindex = 0;
            //第N列
            int columnindex = 0;
            pageControls.Children.Clear();
            for (int i = 0; i < pagecount; i++)
            {
                ButtonEx btn = new ButtonEx()
                {
                    Cursor=Cursors.Hand,
                    Width = PageIndex == i ? 20:10,
                    Height = 10,
                    CornerRadius = "5",
                    MarginLeft=3,
                    BackColor = Color.DodgerBlue,
                    Tag = i
                };
                btn.Click += Btn_Click;
                pageControls.Children.Add(btn);
            }
            for (int i = 0; i < Items.Count; i++)
            {

                Items[i].Width = this.Width.Value / MatrixColumns;
                Items[i].Height = (this.Height.Value-30) / MatrixRows;
                columnindex = Items[i].NewTabPageIndex % MatrixColumns;
                rowindex = (Items[i].NewTabPageIndex) / MatrixColumns % MatrixRows;
                pageindex = Items[i].NewTabPageIndex / matrixCount;
                Items[i].MarginLeft = pageindex * Width.Value + columnindex * Items[i].Width.Value;// new Point(, );
                Items[i].MarginTop = rowindex * Items[i].Height.Value;
       
                Items[i].OldLocation = new Point(Items[i].MarginLeft.Value, Items[i].MarginTop.Value);

            }
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            ButtonEx bt = (ButtonEx)sender;
            for (int i = 0; i < pageControls.Children.Count; i++)
            {
                ButtonEx b = pageControls.Children[i] as ButtonEx;
                if (b != bt)
                {
                    b.Width = 10;
                }
                bt.Width = 20;
                PageIndex = int.Parse(bt.Tag.ToString());
            }
        }
        /// <summary>
        /// 查找当前移动标签所在的标签
        /// </summary>
        /// <param name="item">移动标签</param>
        /// <returns>目标标签</returns>
        public IPadViewItem FindMoveItemToItem(IPadViewItem item)
        {
            Rect r =new Rect(item.MarginLeft.Value,item.MarginTop.Value,item.Width.Value,item.Height.Value);
            Point p=new Point(r.X+ item.Width.Value/2, r.Y+item.Height.Value/2);
            for (int i = 0; i < Items.Count; i++)
            {
                if (item == Items[i])
                {
                    continue;
                }
                Rect temp = new Rect(Items[i].MarginLeft.Value, Items[i].MarginTop.Value, Items[i].Width.Value, Items[i].Height.Value);
                //temp.Intersect(r);
                //if ((temp.Width * temp.Height * 1.0) >= (r.Width * r.Height * 3.0 / 5.0))
                //{
                //    return Items[i];
                //}
                if (IsPointInRectangle(p, temp))
                {
                    return Items[i];
                }
            }
            return null;
        }

        public bool IsPointInRectangle(Point p, Rect rect)
        {
            return (p.X >= rect.Left && p.X <= rect.Right && p.Y >= rect.Top && p.Y <= rect.Bottom);
        }
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
